<?php
// a Flash Client RPC library to DB and a Session Counter

include_once("../../php/db/MySQLiWrapper.php");

//$ts = new FlashClientProcedures();
//$result=$ts->get_properties_list(2);
//echo mysql_result($result, 0, 1);
 
class FlashClientProcedures
	{

	public function FlashClientProcedures()
	    {
	    $this->methodTable = array(
		"get_game_data" => array(
		    "description" => "query for game data rescordset",
		    "access"    => "remote",
		    "arguments" => array(arg1),
		    "returntype"=> "recordSet"
		),
		"get_players_data" => array(
		    "description" => "query for players data rescordset",
		    "access"    => "remote",
		    "arguments" => array(arg1),
		    "returntype"=> "recordSet"
		),
		"get_board_data_by_id" => array(
		    "description" => "query for board data by id rescordset",
		    "access"    => "remote",
		    "arguments" => array(arg1),
		    "returntype"=> "recordSet"
		),
		"get_player_by_name" => array(
                    "description" => "query for player data by name",
                    "access"    => "remote",
                    "arguments" => array(arg1),
                    "returntype"=> "recordSet"
                ),
		"get_properties_list" => array(
		    "description" => "query for properties list rescordset",
		    "access"    => "remote",
		    "arguments" => array(arg1),
		    "returntype"=> "recordSet"
		)
	    );

	    // Check if the session is available or create it.
            if (!isset($_SESSION['count']))
		{
                $_SESSION['count'] = 0;
        	}
	    }

	// general usage function
	public function mquery($query)
	    {
	    $dbconn = new MySQLiWrapper();
	    //$dbconn->connect();
	    $result = $dbconn->MQuery($query);
	    $dbconn->disconnect();
	    
	    return $result;
	    }

	/* the AMFPHP RPC interface */
	
	public function get_game_data($gameId)
	    {
	    //@TP - try stored procedure
	    $query = "call mp_get_game_info($gameId)";
	    
	    return ($this->mquery($query));
	    }

	/* get all players data for game using SP */
	public function get_players_data($gameId)
	    {
	    $query = "call mp_get_gathered_players_list($gameId)";

	    return $this->mquery($query);
	    }
	/* get list of game items */
	public function get_properties_list($gameId)
	    {
	    $query = "call mp_get_game_items($gameId)";
	    return $this->mquery($query);
	    }

	// Used to increment the session variable count.
    	public function increment() 
	    {
            $_SESSION['count']++;
            return $_SESSION['count'];
    	    }
	// used to destroy the session variable and start over.
    	public function unregister()
	    {
            unset($_SESSION['count']);
    	    return true;
    	    }
	public function get_player_by_name($name) 
            {
            $query = "SELECT * FROM users WHERE name='$name'";

            return $this->mquery($query);
            }


	// just a debug function
	public function GameInfo()
	    {
    	    // return Active Game row by game ID
    	    $query = "SELECT * FROM $dbtable WHERE UserStatus='offline'";

	    return mysql_query($query);
	    }

	public function destroy()
	    {
            session_destroy();
    	    return true;
    	    }
	}
?>
